home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Copyright 1994, Silicon Graphics, Inc.
- ** All Rights Reserved.
- **
- ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- ** the contents of this file may not be disclosed to third parties, copied or
- ** duplicated in any form, in whole or in part, without the prior written
- ** permission of Silicon Graphics, Inc.
- **
- ** RESTRICTED RIGHTS LEGEND:
- ** Use, duplication or disclosure by the Government is subject to restrictions
- ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- ** rights reserved under the Copyright Laws of the United States.
- **
- */
- #include <GL/glx.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
-
- static int RGB_attributes[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- None,
- };
-
- static int CI_attributes[] = {
- None,
- };
-
- int rgb = 1;
- int W = 401;
- int H = 401;
-
- float rotangle = 0;
- float width = 1;
- int stippling = 0;
- int anti = 1;
-
- static void Init(void)
- {
- glViewport(0,0,W,H);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-100.0, 100.0, -100.0, 100.0, 0.0, 1.0);
- glMatrixMode(GL_MODELVIEW);
-
- if (rgb) {
- glClearColor(0.15,0.15,0.3,0);
- } else {
- glClearIndex(16);
- }
-
- glClear(GL_COLOR_BUFFER_BIT);
- }
-
- static void Redraw(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glLineWidth(width);
-
- if (rgb) {
- glColor3f(1.0, 1.0, 1.0);
- } else {
- if (anti) {
- glIndexf(0);
- } else {
- glIndexf(15);
- }
- }
- glPushMatrix();
- glRotatef(rotangle, 0, 0, 1);
- glBegin(GL_LINES);
- glVertex3i(-90, 0, 0);
- glVertex3i(90, 0, 0);
- glEnd();
- glPopMatrix();
- glFlush();
- }
-
- static void Usage(void)
- {
- fprintf(stderr, "Usage: program [-c]\n");
- exit(1);
- }
-
- static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- int main(int argc, char** argv)
- {
- XVisualInfo *vi;
- Display *dpy;
- Colormap cmap;
- Window window;
- XSetWindowAttributes swa;
- GLXContext cx;
- XEvent event;
- GLboolean needDisplay;
- XColor white;
- int i;
-
- rgb = 1;
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'c':
- rgb = GL_FALSE;
- break;
- default:
- Usage();
- }
- } else {
- Usage();
- }
- }
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
- return -1;
- }
-
- vi = glXChooseVisual(dpy, DefaultScreen(dpy),
- rgb ? RGB_attributes : CI_attributes);
- if (!vi) {
- fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- if (rgb) {
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- AllocNone);
- white.red = ~0;
- white.green = ~0;
- white.blue = ~0;
- XAllocColor(dpy, cmap, &white);
- swa.background_pixel = white.pixel;
- } else {
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- AllocAll);
- swa.background_pixel = 15;
- }
-
- swa.border_pixel = 0;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- W, H,
- 0, vi->depth, InputOutput, vi->visual,
- CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
- &swa);
- XSetStandardProperties(dpy, window, "OpenGL Antialiased Line demo", "aaline",
- None, argv, argc, NULL);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
-
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
-
- if (!rgb) {
- XColor buf;
- int i;
-
- buf.flags = DoRed | DoGreen | DoBlue;
-
- /* Init color map */
- for (i=0; i<16; i++) {
- buf.pixel = i;
- buf.red = i*(65535/15);
- buf.green = i*(65535/15);
- buf.blue = i*(65535/15);
- XStoreColor(dpy, cmap, &buf);
- }
- buf.pixel = 16;
- buf.red = 10000;
- buf.green = 10000;
- buf.blue = 20000;
- XStoreColor(dpy, cmap, &buf);
- }
-
- Init();
- glEnable(GL_LINE_SMOOTH);
- if (rgb) {
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
- }
-
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- W = event.xconfigure.width;
- H = event.xconfigure.height;
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
- GLboolean setNeed = GL_TRUE;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_O: case XK_o:
- rotangle += 1;
- break;
- case XK_P: case XK_p:
- rotangle -= 1;
- break;
- case XK_Q: case XK_q:
- width -= 0.125;
- if (width < 1) width = 1;
- break;
- case XK_W: case XK_w:
- width += 0.125;
- break;
- case XK_A: case XK_a:
- anti = 1-anti;
- if (anti) {
- glEnable(GL_LINE_SMOOTH);
- if (rgb) {
- glEnable(GL_BLEND);
- }
- } else {
- glDisable(GL_LINE_SMOOTH);
- if (rgb) {
- glDisable(GL_BLEND);
- }
- }
- break;
- case XK_L: case XK_l:
- stippling = 1-stippling;
- if (stippling) {
- glEnable(GL_LINE_STIPPLE);
- glLineStipple(2, 0xf090);
- } else {
- glDisable(GL_LINE_STIPPLE);
- }
- break;
- case XK_Escape:
- return 0;
- default:
- setNeed = GL_FALSE;
- break;
- }
- if (setNeed) needDisplay = GL_TRUE;
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- Redraw();
- }
- }
- }
-